<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" >
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="请输入昵称/手机账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="resetting()">重置</el-button>
        <el-button @click="dialogVisible=true;score=''" v-if="isAuth('user:score:update')">积分调整</el-button>
        <!-- <el-button v-if="isAuth('course:coursetype:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="序号"
      >
      <template slot-scope="scope">
          <div v-if="scope.row">
            {{ scope.$index+1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="phoneNumber" header-align="center" align="center" label="手机号码">
      </el-table-column>
      <el-table-column prop="nickName" header-align="center" align="center" label="昵称" width="250">
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="最后变更时间">
        <template slot-scope="scope">
          <div v-if="scope.row">
            {{ getStrTime(scope.row.regDate) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="score" header-align="center" align="center" label="积分余额">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="details(scope.row.id)" v-if="isAuth('user:score:record:page')">积分明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <AddOrUpdates v-if="addOrUpdateVisibles" ref="addOrUpdates" @refreshDataList="getDataList"></AddOrUpdates>
    <el-dialog title="积分调整" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="dataForm" label-width="100px">
        <el-form-item label="调整积分：">
          <el-input v-model="score" placeholder="正数为加，负数为减" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="scoreUpdateFun()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdates from "./details";

export default {
  data() {
    return {
      score:'',
      dialogVisible: false,
      dataForm: {
        key: "",
        status: null,
        type: null,
      },
      memberStatus: null,
      options: [
        {
          value: null,
          label: "全部",
        },
        {
          value: 1,
          label: "正常",
        },
        {
          value: 0,
          label: "禁用",
        },
      ],
      option1: [
        {
          value: 1,
          label: "正常",
        },
        {
          value: 0,
          label: "禁用",
        },
      ],
      optionTwo: [
        {
          value: null,
          label: "全部",
        },
        {
          value: 1,
          label: "内部",
        },
        {
          value: 2,
          label: "外部",
        },
      ],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      addOrUpdateVisibles: false,
    };
  },
  components: {
    AddOrUpdates,
  },
  mounted() {
    this.$forceUpdate()
  },
  activated() {
    this.getDataList();
  },
  methods: {
    cmtSet() {
      if (this.dataListSelections.length > 0) {
        var ids = this.dataListSelections.map((item, index) => {
          return item.memberId;
        });
        this.uStatus(ids, this.memberStatus);
      } else {
        this.$message.error("请选择至少一个用户");
      }
    },
    handleClose(done) {
      done();
    },
    resetting() {
      this.dataForm = {
        key: "",
        status: null,
        type: null,
      }
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      var type = {};
      var status = {};
      this.dataListLoading = true;
      if (this.dataForm.type !== null) {
        type.type = this.dataForm.type;
      }
      if (this.dataForm.status !== null) {
        status.status = this.dataForm.status;
      }
      this.$http({
        url: this.$http.adornUrl(`/admin/user/balance/page`),
        method: "post",
        data: this.$http.adornData({
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          phoneNumber: this.dataForm.key,
          ...type,
          ...status,
        }),
      }).then(({ data }) => {
        if (data && data.code === "200") {
          this.dataList = data.list;
          console.log(this.dataList);
          this.totalPage = data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    details(id) {
      console.log(id)
      this.addOrUpdateVisibles = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdates.init(id);
      });
    },
    scoreUpdateFun(){
      if(!this.dataListSelections.length){
        this.$message.error("请选择要调整积分的选项");
        return
      }
      if(!this.score){
        this.$message.error("请输入调整积分");
        return
      }
      var ids = this.dataListSelections.map((item) => {
        return item.id
      })
      this.$http({
          url: this.$http.adornUrl("/admin/user/score/update"),
          method: "post",
          data: this.$http.adornData({ ids: ids, score: this.score }, false),
        }).then(({ data }) => {
          if (data && data.code === "200") {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
                this.dialogVisible=false
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
    },
   
    // 删除
    uStatus(id, e) {
      this.$confirm(
        `确定要 ${e==1
          ? "启用？启用后将恢复登录前端平台权限"
          : "禁用？禁用后会员将无法登录前端平台，数据不受影响"
        }`,
        `会员${e==1 ? "启用" : "禁用"} `,
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "danger",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl(e==1?"/admin/user/status/enable":'/admin/user/status/disable'),
          method: "post",
          data: this.$http.adornData({ id: id, status: e }, false),
        }).then(({ data }) => {
          if (data && data.code === "200") {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.info {
  display: flex;
  justify-content: center;
  align-items: center;

  .img {
    width: 40px;
    height: 40px;
    border: 1px solid #999;
    border-radius: 50%;
    overflow: hidden;
    margin-right: 10px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .rightCo {
    .title {
      color: #333;
      font-size: 14px;
    }

    .phone {
      color: #333;
      font-size: 14px;
    }
  }
}
</style>
